<script setup lang="ts">
import Content from '@/components/Content/index.vue'
import ContentTitle from '@/components/ContentTitle/index.vue'
import EditAvatar from './components/EditAvatar/index.vue'
import EditUsername from './components/EditUsername/index.vue'
import EditPassword from './components/EditPassword/index.vue'
import EditEmail from './components/EditEmail/index.vue'
import EditWeChat from './components/EditWeChat/index.vue'
import { Plus } from '@element-plus/icons-vue'
import APIInformation from '@/api/information'
import type { unbindPersonalType } from '@/api/information/types'
import { ElMessage, ElMessageBox, ElLoading } from 'element-plus'
import { useAuth } from '@/stores/index'
import { replaceCommaWithDash } from '@/utils/index'

const { getInfo } = useAuth()
const { info } = storeToRefs(useAuth())

const dialogAvatarVisible = shallowRef<boolean>(false)

const dialogUsernameVisible = shallowRef<boolean>(false)

const dialogPasswordVisible = shallowRef<boolean>(false)

const dialogEmailVisible = shallowRef<boolean>(false)

const dialogWeChatVisible = shallowRef<boolean>(false)

function submitUnbind(type: unbindPersonalType) {
    let text: string = ''
    if (type == 'email') {
        text = '您将解除已绑定邮箱，稍后可重新鄉定！'
    } else if (type == 'password') {
        text = '您将解除密码登陆功能，密码登陆不再可用！'
    } else if (type == 'webchat') {
        text = '您将解除微信登陆功能，微信不再可用！'
    }

    let loading: any
    ElMessageBox.confirm(
        text,
        '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(() => {
        loading = ElLoading.service({ lock: true })
        return APIInformation.unbindPersonal(type)
    }).then(({ msg }) => {
        ElMessage.success(msg)
        getInfo()
    }).catch(() => {
        ElMessage.info('已取消')
    }).finally(() => {
        loading?.close()
    })
}


const dialogVisible = shallowRef<boolean>(false)
const $router = useRouter()

function handleJumpInfoPersonal() {
    $router.push({
        name: 'InfoPersonal'
    })
}

function handleJumpInfoCompany() {
    $router.push({
        name: 'InfoCompany'
    })
}


</script>

<template>
    <div class="information">
        <Content class="content">
            <ContentTitle title="基本信息"
                content="1、用户名作为您在心安元素的身份，只能修改一次；设置后无法修改；<br />2、如入住店铺，用户名会作为您的店铺名称对外展示，请知晓；<br />3、实名认证涉及到您提现的一致性，请认证对待；" />
            <div class="userinfo">
                <div class="left">
                    <div @click="dialogAvatarVisible = true">
                        <img v-if="info.head_img" :src="info.head_img" alt="头像" decoding="async">
                        <el-skeleton animated>
                            <template #template>
                                <el-skeleton-item variant="image" />
                            </template>
                        </el-skeleton>
                    </div>
                </div>
                <ul class="right">
                    <li>
                        <div class="label">用户名</div>
                        <div class="value info"><span>：</span>{{ info.username }}</div>
                        <div class="value primary edit-btn" v-if="info.is_update != 1"
                            @click="dialogUsernameVisible = true">修改</div>
                    </li>
                    <li>
                        <div class="label">实名</div>
                        <div class="value primary"><span>：</span>
                            <span class="primary" v-if="info.is_auth == 0">未实名认证</span>
                            <span class="primary" v-else-if="info.is_auth == 1">已实名认证</span>
                        </div>
                    </li>
                    <li>
                        <div class="label">账号ID</div>
                        <div class="value info"><span>：</span>{{ info.Invite_code }}</div>
                    </li>
                    <li>
                        <div class="label">钱包</div>
                        <div class="value danger"><span>：</span>{{ info.balance || 0 }}</div>
                    </li>
                    <li>
                        <div class="label">注册</div>
                        <div class="value info"><span>：</span>{{ info.create_time }}</div>
                    </li>
                    <li>
                        <div class="label">IP所在</div>
                        <div class="value info"><span>：</span> {{ replaceCommaWithDash(info.area) }}</div>
                    </li>
                </ul>
            </div>
        </Content>
        <Content class="content">
            <ContentTitle title="绑定信息" content="绑定后可以用来快速登录和接收信息；" />
            <div class="binding width">
                <div class="item">
                    <div class="label ">手 机：</div>
                    <div class="value info">{{ info.phone }}</div>
                    <div class="value primary" v-if="info.is_phone == 1">已绑定</div>
                    <div class="value danger" v-else>未绑定</div>
                </div>
                <div class="item">
                    <div class="label">微 信：</div>
                    <div class="value primary" v-if="info.is_webchat == 1" @click="submitUnbind('webchat')">已绑定</div>
                    <div class="value danger" v-else @click="dialogWeChatVisible = true">未绑定</div>
                </div>
                <div class="item">
                    <div class="label">邮 箱：</div>
                    <div class="value primary" v-if="info.is_email == 1" @click="submitUnbind('email')">已绑定</div>
                    <div class="value danger" v-else @click="dialogEmailVisible = true">未绑定</div>
                </div>
            </div>
        </Content>
        <Content class="content">
            <ContentTitle :title="`认证信息（${info.is_auth == 1 ? '1' : '0'}/1）`"
                content="1、认证信息涉及到此账号的归宿问题，请认证对待；<br />2、认证可以是个人，也可以是公司认证；<br />3、需要注意的是认证为个人，提现只能提现到公司；认证为公司，只能提现到公司；<br />4、认证信息不能更改；" />
            <div class="auth width">
                <div class="left">
                    <span v-if="info.user_type == 'personal'">{{ info.auth_info?.real_name }}</span>
                    <span v-if="info.user_type == 'company'">{{ info.auth_info?.company_name }}</span>
                    <span v-if="info.user_type == 'personal'">（个人）</span>
                    <span v-if="info.user_type == 'company'">（公司）</span>
                    <span v-if="info.user_type == 'personal'"> {{ info.auth_info?.id_card }}</span>
                    <span v-if="info.user_type == 'company'"> {{ info.auth_info?.registration_code }}</span>
                </div>
                <button class="right" v-if="info.is_auth == 0" @click="dialogVisible = true">
                    <el-icon color="#FE0000">
                        <Plus />
                    </el-icon>添加认证信息
                </button>
                <button class="right primary" v-if="info.is_auth == 1">
                    已认证
                </button>
            </div>
        </Content>
        <Content class="content">
            <ContentTitle title="登录信息" content="绑定后可以用密码来快速登录；" />
            <div class="logininfo width">
                <div class="label">密码登录：</div>
                <div class="value primary" v-if="info.is_password == 1" @click="submitUnbind('password')">已绑定</div>
                <div class="value danger" v-else @click="dialogPasswordVisible = true">未绑定</div>
            </div>
        </Content>
        <EditAvatar v-model="dialogAvatarVisible" :image="info.head_img" />
        <EditUsername v-model="dialogUsernameVisible" />
        <EditPassword v-model="dialogPasswordVisible" />
        <EditEmail v-model="dialogEmailVisible" />
        <EditWeChat v-model="dialogWeChatVisible" />
        <el-dialog v-model="dialogVisible" title="选择验证的信息类型" width="600" :show-close="false">
            <template #header>
                <DialogClose title="选择验证的信息类型" @close="dialogVisible = false" />
            </template>
            <div class="dialog-btn">
                <button @click="handleJumpInfoPersonal">个人认证</button>
                <button @click="handleJumpInfoCompany">公司 | 机构 | 组织认证</button>
            </div>

            <div class="notice">注意：每个账号可以以个人或公司分别认证，以第一个认证为归宿账号。2种信息。</div>
        </el-dialog>
    </div>
</template>

<style lang="scss" scoped>
.information {

    .dialog-btn {
        display: flex;
        flex-direction: column;
        gap: 34px 0;
        padding: 20px 0;

        .notice {
            color: rgba(190, 190, 190, 1);
            font-size: 14px;
        }

        button {
            width: 307px;
            height: 44px;
            border-radius: 5px 5px 5px 5px;
            background-color: rgba(220, 221, 221, 1);
            color: rgba(136, 136, 136, 1);
            font-size: 14px;
            border: none;
            cursor: pointer;
            transition: all .2s;

            &:hover {
                color: #ffffff;
                background-color: #1684FC;
            }
        }
    }

    .danger {
        color: #F91414;
        cursor: pointer;
        user-select: none;
    }

    .info {
        color: rgba(154, 154, 154, 1);
    }

    .width {
        width: 94%;
        margin: 0 auto;
        margin-top: 20px;
        margin-bottom: 30px;
    }

    .content {
        margin-bottom: 30px;

        .logininfo {
            display: flex;

            .label {
                font-family: 'SOURCEHANSANSCN-LIGHT';
            }

            .value {
                font-family: 'SOURCEHANSANSCN-EXTRALIGHT';
            }
        }

        .auth {
            border: 1px solid #E3E3E3;
            display: flex;

            .right {
                background-color: #ffffff;
                border: none;
                width: 155px;
                height: 48px;
                font-size: 14px;
                border-left: 1px solid #E3E3E3;
                display: flex;
                align-items: center;
                justify-content: center;
                gap: 0 10px;
                cursor: pointer;
                font-family: 'SOURCEHANSANSCN-EXTRALIGHT';
            }

            .left {
                width: calc(100% - 155px);
                display: flex;
                align-items: center;
                padding-left: 20px;
                color: rgb(104, 104, 104);
                font-family: 'SOURCEHANSANSCN-LIGHT';
            }
        }

        .binding {
            display: grid;
            grid-template-columns: repeat(3, 1fr);

            .item {
                display: flex;
                align-items: center;
                white-space: nowrap;

                .value {
                    font-family: 'SOURCEHANSANSCN-LIGHT';
                }

                .label {
                    font-family: 'SOURCEHANSANSCN-REGULAR';
                }
            }
        }

        .userinfo {
            display: flex;

            .right {
                flex: 4;
                list-style: none;
                display: flex;
                flex-direction: column;

                li {
                    display: flex;

                    .label {
                        color: rgb(16, 16, 16);
                        font-size: 14px;
                        width: 46px;
                        text-align: justify;
                        font-family: 'SOURCEHANSANSCN-LIGHT';

                        &::after {
                            content: "";
                            display: inline-block;
                            width: 100%;

                        }
                    }

                    .value {
                        font-size: 14px;
                        font-family: 'SOURCEHANSANSCN-EXTRALIGHT';

                        span {
                            color: rgb(16, 16, 16);
                        }
                    }


                    .edit-btn {
                        margin-left: 12px;
                        cursor: pointer;
                    }
                }
            }

            .left {
                flex: 6;
                display: flex;
                align-items: center;
                justify-content: center;

                div {
                    width: 150px;
                    height: 150px;
                    background-color: #d7d7d7;
                    border-radius: 5px;
                    overflow: hidden;
                    cursor: pointer;

                    img {
                        width: 100%;
                        height: 100%;
                    }
                }
            }
        }
    }

    .primary {
        color: #1684FC !important;
        cursor: pointer;
        user-select: none;
    }
}
</style>
